import requests
from bs4 import BeautifulSoup
import os

# 福利院
url = 'https://www.dongmanmanhua.cn/SUSPENSE/fuliyuan/%E7%AC%AC1%E8%AF%9D/viewer?title_no=2117&episode_no=1'


# 模拟浏览器请求头部信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299',
    'Referer': 'https://www.dongmanmanhua.cn/',
}

# 发送get请求，获取响应内容
response = requests.get(url, headers=headers)
html = response.text

# 使用beautifulsoup解析html
soup = BeautifulSoup(html, 'html.parser')

# 找到所有img标签
img_tags = soup.find_all('img')

# 遍历img标签列表，获取每个图片的url并下载
for i, img_tag in enumerate(img_tags):
    img_url = img_tag.get('data-url')
    
    # 如果img_url为None，跳过此次循环
    if not img_url:
        continue
    
    img_name = f".\\images\\image{i}.jpg"
    response = requests.get(img_url, headers=headers)
    with open(img_name, 'wb') as f:
        f.write(response.content)
        print(f"Downloaded {img_name} successfully!")



folder = 'images/'
def count_files(dir_path):
    return len([f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))])

print('图片总数:', count_files(folder))